//
//  FilmCollectionViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FilmCollectionViewController.h"
#import "FilmDetailsViewController.h"
#import "FilmCollectionViewCell.h"
#import "netWorkHandle.h"
#import "Movie.h"
#import "UIImageView+WebCache.h"

@interface FilmCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionV;

@end

@implementation FilmCollectionViewController

- (void)dealloc
{                                                                                                                                                                                                                                                  
    [_collectionV release];
    [_movieArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
  /******************************** 电影缩略图界面 *************************************/

    
    self.navigationItem.title = @"电影";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
        self.navigationItem.hidesBackButton = YES;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 110) collectionViewLayout:layout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionV];
    [_collectionV release];
    
    [self.collectionV registerClass:[FilmCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Movie *movie = [self.movieArr objectAtIndex:indexPath.row];
    
    FilmDetailsViewController *filmDC = [[FilmDetailsViewController alloc] init];
    
    filmDC.movie = movie;
    
    [self.navigationController pushViewController:filmDC animated:YES];
    
    [filmDC release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.view.frame.size.width - 40) / 3.0, (self.view.frame.size.height - 40) / 3.0);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
   FilmCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Movie *movie = [self.movieArr objectAtIndex:indexPath.row];
    
    [cell.imageV sd_setImageWithURLStr:movie.image];
    cell.label.text = movie.movieName;
    
    return  cell;
    
}

- (void)rightAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
